import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

public class normal {


    private void sendDataByPayload(byte[] data) {
        System.out.println(Arrays.toString(data));
    }


    public void track(String src, int freq_point, int deviceId, int period) {


//        untrack();
//        if (freq_point > 0) {
//            removeDeviceFreqPoint(deviceId);
//            addFreqPoint(freq_point + "", deviceId, period);
//        }


        byte[] sendByte = new byte[26];
        sendByte[0] = 0x35;
        sendByte[1] = 26;
        char[] chars = src.toCharArray();
        int j = 0;
        for (char c : chars) {
            sendByte[j + 2] = (byte) Integer.valueOf(c).intValue();
            j = j + 1;
        }

        sendDataByPayload(sendByte);


    }

    public void untrack() {
        byte[] sendByte = new byte[2];
        sendByte[0] = 0x36;
        sendByte[1] = 2;

        sendDataByPayload(sendByte);

    }

    public void addFreqPoint(String src, int deviceId, int period) {

        char[] chars = src.toCharArray();
        byte[] sendByte = new byte[14];
        sendByte[0] = 0x37;
        sendByte[1] = 14;

        int value = deviceId;
        for (int i = 0; i < 4; i++) {
            sendByte[2 + i] = new Integer(value).byteValue();
            value = value >> 8;
        }

        value = Integer.valueOf(src);
        for (int i = 0; i < 4; i++) {
            sendByte[6 + i] = new Integer(value).byteValue();
            value = value >> 8;
        }
        value = period;
        for (int i = 0; i < 4; i++) {
            sendByte[10 + i] = new Integer(value).byteValue();
            value = value >> 8;
        }


        sendDataByPayload(sendByte);


    }

    public void removeDeviceFreqPoint(int did) {

        removeFreqPoint("0", did);


    }

    public void removeAllFreqPoint() {
        removeFreqPoint("0", 0);
        removeFreqPoint("0", 1);
    }


    public void removeFreqPoint(String src, int tdeviceId) {

        char[] chars = src.toCharArray();
        byte[] sendByte = new byte[10];
        sendByte[0] = 0x38;
        sendByte[1] = 10;

        int value = tdeviceId;
        for (int i = 0; i < 4; i++) {
            sendByte[2 + i] = new Integer(value).byteValue();
            value = value >> 8;
        }

        value = 0;
        for (int i = 0; i < 4; i++) {
            sendByte[6 + i] = new Integer(value).byteValue();
            value = value >> 8;
        }

        sendDataByPayload(sendByte);

    }

    public void reset(int deviceId) {

        byte[] sendByte = new byte[6];
        sendByte[0] = 0x39;
        sendByte[1] = 6;


        int value = deviceId;
        for (int i = 0; i < 4; i++) {
            sendByte[2 + i] = new Integer(value).byteValue();
            value = value >> 8;
        }

        sendDataByPayload(sendByte);
    }


    private void testAddFreqPoint(){
        normal normal = new normal();
        normal.addFreqPoint("38400", 12, 30);

        ByteArrayOutputStream bOut = new ByteArrayOutputStream(1024);
        CDataOutputStream dataOutputStream = new CDataOutputStream(bOut);

        ByteBuffer byteBuffer = ByteBuffer.allocate(14);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.put((byte) 0x37);
        byteBuffer.put((byte) 14);
        byteBuffer.putInt(12);
        byteBuffer.putInt(38400);
        byteBuffer.putInt(30);

        System.out.println("byteBuffer");
        System.out.println(Arrays.toString(byteBuffer.array()));

        ByteBuffer read = ByteBuffer.wrap(byteBuffer.array());
        read.order(ByteOrder.LITTLE_ENDIAN);
        System.out.println(read.get());
        System.out.println(read.get());
        System.out.println(read.getInt());
        System.out.println(read.getInt());
        System.out.println(read.getInt());
    }

    public static void main(String[] args) {
        normal normal = new normal();
        normal.track("460001357924680",38400, 12,30);
        ByteBuffer byteBuffer = ByteBuffer.allocate(26);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byteBuffer.put((byte) 0x35);
        byteBuffer.put((byte) 26);
        char[] chars = "460001357924680".toCharArray();

        int j = 0;
        for (char c : chars) {
            byte d = Integer.valueOf(c).byteValue();
            byteBuffer.put(d);
            j = j + 1;
        }
        System.out.println(Arrays.toString(byteBuffer.array()));


        CustomByteBuffer customByteBuffer = new CustomByteBuffer(26);
        customByteBuffer.writeByte(0x35);
        customByteBuffer.writeByte(26);
        customByteBuffer.writeInt(566);
        customByteBuffer.writeString("460001357924680");
        System.out.println(customByteBuffer);

        CustomByteBuffer reader = new CustomByteBuffer(customByteBuffer.toArray());
        System.out.println(reader.readByte());
        System.out.println(reader.readByte());
        System.out.println(reader.readIntByByte());
        System.out.println(reader.readString(15));


    }
}
